﻿using Furion.DatabaseAccessor;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;

namespace Mrhd.Core.Entities
{
    /// <summary>
    /// 移动端用户与商户
    /// </summary>
    [Table("Sys_UserMobileToBusiness")]
    [Comment("移动端用户与商户")]
    public class UserMobileToBusinessEntity : IEntity, IEntityTypeBuilder<UserMobileToBusinessEntity>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        /// <summary>
        /// 用户id
        /// </summary>
        [Comment("用户id")]
        public int UserId { get; set; }
        /// <summary>
        /// 用户实体
        /// </summary>
        public virtual UserMobileEntity User { get; set; }
        /// <summary>
        /// 商户id
        /// </summary>
        [Comment("商户id")]
        public int BusinessId { get; set; }
        /// <summary>
        /// 商户实体
        /// </summary>
        public virtual BusinessEntity Business { get; set; }

        public void Configure(EntityTypeBuilder<UserMobileToBusinessEntity> entityBuilder, DbContext dbContext, Type dbContextLocator)
        {
            entityBuilder
                .HasOne(a => a.Business)
                .WithMany(a => a.WxUsers)
                .HasForeignKey(a => a.BusinessId)
                .OnDelete(DeleteBehavior.ClientCascade);
            entityBuilder
                .HasOne(a => a.User)
                .WithMany(a => a.Businesses)
                .HasForeignKey(a => a.UserId)
                .OnDelete(DeleteBehavior.ClientCascade);
        }
    }

}
